let url = 'http://www.kuwo.cn/api/www/classify/playlist/getRcmPlayList?pn=1&rn=20&order=new&httpsStatus=1&reqId=1e1f0b70-e360-11ed-b6bd-bbf4def6f09a'


// 歌单
async function playList(url) {
    let f1 = fetch(replaceHost(url))
    let f2 = f1.then(res => res.json())
    let f3 = await f2.then((data) => {
        // 歌单列表
        $('.loading').eq(1).hide()
        List(data.data.data)
        return data.data.total
    })
    this.count = f3
}



// 歌单列表渲染函数
function List(data) {
    $('.list').html('')
    data.forEach((v, i) => {
        $(`            <div class="playlist-item">
        <div class="pic_out play-list" id=${v.id}>
            <img data-src=${v.img} alt="">
            <div class="mack">
                <span>
                    <i class="iconfont icon-bofang-01"></i>
                </span>
            </div>
        </div>
        <div class="text">
            <h3 class="item-title play-list" id=${v.id}>${v.name}</h3>
            <p><i class="iconfont icon-bofang"></i>${count(v.listencnt)}</p>
        </div>
        </div>`).appendTo($('.list'))
    });
    load()
    // 歌单跳转
    let playList = document.getElementsByClassName('play-list')
    Array.from(playList).forEach((v, i) => {
        v.onclick = function () {
            location.href = `./playlist_detail.html?id=${v.id}`
        }
    })
}

// 格式化粉丝
function count(num) {
    return parseInt(num / 10000) ? '' + parseInt(num / 10000) + '.' + parseInt(num % 10000 / 100) + '万' : num
}


// 最新最热点击事件
$('.title > p').on('click', function () {
    $('.title > .this').removeClass('this')
    $(this).addClass('this')

    if ($(this).index() === 2) {
        url = 'http://www.kuwo.cn/api/www/classify/playlist/getRcmPlayList?pn=1&rn=20&order=hot&httpsStatus=1&reqId=7143f870-e27e-11ed-80ce-0752fda79b69'
    } else {
        url = 'http://www.kuwo.cn/api/www/classify/playlist/getRcmPlayList?pn=1&rn=20&order=new&httpsStatus=1&reqId=3d1338e0-e27e-11ed-80ce-0752fda79b69'
    }
    // 分页
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        //执行一个laypage实例
        laypage.render({
            elem: 'test1'
            , count: 200
            , prev: '<em class="iconfont icon-arrow-left"></em>'
            , next: '<em class="iconfont icon-arrow-right"></em>'
            , jump: function (obj, first) {
                $('.list').html(`            <!-- loading -->
                <div class="loading">
                    <span class="loader"></span>
                </div>`)

                $('.loading').eq(0).show()

                playList.apply(this, [myUrl(url, obj.curr)])
            }
        });

    });
})


// 导航栏点击
$('.ku-nav > li').eq(0).on('click', function () {
    location.href = './index.html'
})

$('.ku-nav > li').eq(1).on('click', function () {
    location.href = './rankList.html'
})

$('.ku-nav > li').eq(2).on('click', function () {
    location.href = './singers.html'
})

$('.ku-nav > li').eq(3).on('click', function () {
    location.href = './playlists.html'
})

$('.ku-nav > li').eq(4).on('click', function () {
    location.href = './mvs.html'
})
$('.ku-logo').on('click', function () {
    location.href = './index.html'
})


// 分页
layui.use('laypage', function () {
    var laypage = layui.laypage;
    //执行一个laypage实例
    laypage.render({
        elem: 'test1'
        , count: 200
        , limit: 20
        , prev: '<em class="iconfont icon-arrow-left"></em>'
        , next: '<em class="iconfont icon-arrow-right"></em>'
        , jump: function (obj, first) {
            $('.list').html(`            <!-- loading -->
            <div class="loading">
                <span class="loader"></span>
            </div>`)

            $('.loading').eq(0).show()
            playList.apply(this, [myUrl(url, obj.curr)])
        }
    });

});


function load() {
    $("img").lazyload({
        threshold: -100, //临界值
        failure_limit: 0,
        event: "scroll",
        effect: "show",
        container: window,
        data_attribute: "src", // 存放真实地址data—
        skip_invisible: false,

        placeholder: "./img/loading.gif", // 显示的默认图片
    });
}